﻿using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Microsoft.IdentityModel.Tokens;
using SqlSugar.Extensions;
using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Security;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
using WebApi.Core.Model.ResponseModels;

namespace WebApi.Core.Common.HttpContextUser
{
    public class AspNetUser : IUser
    {
        private readonly IHttpContextAccessor _accessor; 
        public AspNetUser(IHttpContextAccessor accessor, ILogger<AspNetUser> logger)
        {
            _accessor = accessor; 
        }
        
        public string Name => _accessor.HttpContext?.User.Identity?.Name;

        public int ID => int.Parse(_accessor.HttpContext?.User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier)?.Value ?? "0");
        
        public bool IsAuthenticated() => _accessor.HttpContext?.User?.Identity?.IsAuthenticated ?? false;

        public string GetToken() => _accessor.HttpContext?.Request.Headers["Authorization"].ToString().Replace("Bearer ", "");

        public ApiResponse<string> ApiResponse { get; set; }
        
    }
}
